package com.labot.dao.hibernate;

import java.util.List;

import javax.persistence.Table;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import com.labot.dao.SearchException;
import com.labot.dao.UserDao;
import com.labot.model.EnvioBitacora;
import com.labot.model.User;
import com.labot.model.Voucher;

/**
 * This class interacts with Hibernate session to save/delete and
 * retrieve User objects.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 *   Modified by <a href="mailto:dan@getrolling.com">Dan Kibler</a>
 *   Extended to implement Acegi UserDetailsService interface by David Carter david@carter.net
 *   Modified by <a href="mailto:bwnoll@gmail.com">Bryan Noll</a> to work with
 *   the new BaseDaoHibernate implementation that uses generics.
 *   Modified by jgarcia (updated to hibernate 4)
*/
@Repository("userDao")
public class UserDaoHibernate extends HistoryTackerDaoHibernate<User, Long> implements UserDao, UserDetailsService {

    /**
     * Constructor that sets the entity to User.class.
     */
    public UserDaoHibernate() {
        super(User.class);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<User> getUsers() {
        Query qry = getSession().createQuery("from User u order by upper(u.username)");
        return qry.list();
    }

    /**
     * {@inheritDoc}
     */
    public User saveUser(User user) {
        if (log.isDebugEnabled()) {
            log.debug("user's id: " + user.getId());
        }
        super.save(user);
        // necessary to throw a DataIntegrityViolation and catch it in UserManager
        getSession().flush();
        return user;
    }

    /**
     * Overridden simply to call the saveUser method. This is happening
     * because saveUser flushes the session and saveObject of BaseDaoHibernate
     * does not.
     *
     * @param user the user to save
     * @return the modified user (with a primary key set if they're new)
     */
    @Override
    public User save(User user) {
        return this.saveUser(user);
    }

    /**
     * {@inheritDoc}
    */
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    	List<?> users = getSession().createCriteria(User.class).add(Restrictions.eq("username", username)).list();
        if (users == null || users.isEmpty()) {
            throw new UsernameNotFoundException("user '" + username + "' not found...");
        } else {
            return (UserDetails)users.get(0); 
        }
    }

    /**
     * {@inheritDoc}
    */
    public String getUserPassword(Long userId) {
        JdbcTemplate jdbcTemplate =
                new JdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
        Table table = AnnotationUtils.findAnnotation(User.class, Table.class);
        return jdbcTemplate.queryForObject(
                "select password from " + table.name() + " where id=?", String.class, userId);
    }
    
    @Override
	@SuppressWarnings(value="unchecked")
	public List<User> search(String searchTerm) throws SearchException {
		if (searchTerm != null) {
			searchTerm = "%" + searchTerm + "%";
			Criteria criteria = getSession().createCriteria(User.class);
			
			Criterion rest1= Restrictions.ilike("username", searchTerm);
			Criterion rest2= Restrictions.ilike("fullName", searchTerm);
			Criterion rest3= Restrictions.ilike("email", searchTerm);
			Criterion rest4= Restrictions.ilike("phoneNumber", searchTerm);
			
			criteria.add(Restrictions.or(rest1, rest2, rest3, rest4));
			
			return criteria.addOrder(Order.asc("username"))
									.list();
		} else {
			throw new SearchException("SearchTerm must not be NULL");
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Voucher> getBitacoras(Long  userId) {
		Query qry = getSession().createQuery("from Voucher b WHERE b.idEnvio = :userId order by upper(b.fecha)");
		qry.setParameter(":userId", userId);
		return (List<Voucher>) qry.list();
	}

}
